//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SABRInterpolation.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Interpolation.h>
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (boost::shared_ptr<QuantLib::SABRInterpolation>& childNative, Object^ owner) : CInterpolation(CSABRInterpolation::typeid)
{
#ifdef HANDLE
	_phSABRInterpolation = NULL;
#endif
	_ppSABRInterpolation = &childNative;
    _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
}
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (QuantLib::SABRInterpolation& childNative, Object^ owner) : CInterpolation(CSABRInterpolation::typeid)
{
#ifdef HANDLE
	_phSABRInterpolation = NULL;
#endif
	_ppSABRInterpolation = new boost::shared_ptr<QuantLib::SABRInterpolation> (&childNative);
    _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
    _SABRInterpolationOwner = owner;
    _InterpolationOwner = owner;
}

Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (CSABRInterpolation^ copy) : CInterpolation(CSABRInterpolation::typeid)
{
#ifdef HANDLE
	_phSABRInterpolation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSABRInterpolation = new boost::shared_ptr<QuantLib::SABRInterpolation> (copy->GetShared());
        _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
    }
}
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (System::Type^ t) : CInterpolation(CSABRInterpolation::typeid)
{
#ifdef HANDLE
	_phSABRInterpolation = NULL;
#endif
	if (!t->IsSubclassOf(CSABRInterpolation::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (QuantLib::Handle<QuantLib::SABRInterpolation>& childNative, Object^ owner)  : CInterpolation(CSABRInterpolation::typeid)
{
	_phSABRInterpolation = &childNative;
	_ppSABRInterpolation = &static_cast<boost::shared_ptr<QuantLib::SABRInterpolation>>(childNative.currentLink());
    _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
    _SABRInterpolationOwner = owner;
}
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (QuantLib::Handle<QuantLib::SABRInterpolation> childNative)  : CInterpolation(CSABRInterpolation::typeid)
{
	_phSABRInterpolation = &childNative;
	_ppSABRInterpolation = &static_cast<boost::shared_ptr<QuantLib::SABRInterpolation>>(childNative.currentLink());
    _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CSABRInterpolation::CSABRInterpolation (QuantLib::SABRInterpolation childNative)  : CInterpolation(CSABRInterpolation::typeid)
{
#ifdef HANDLE
	_phSABRInterpolation = NULL;
#endif
	_ppSABRInterpolation = new boost::shared_ptr<QuantLib::SABRInterpolation> (new QuantLib::SABRInterpolation (childNative));
    _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (boost::dynamic_pointer_cast<QuantLib::Interpolation> (*_ppSABRInterpolation));
}
#endif

Cephei::QL::Math::Interpolations::CSABRInterpolation::~CSABRInterpolation ()
{
    if (_ppSABRInterpolation != NULL)
    {
	    delete _ppSABRInterpolation;
        _ppSABRInterpolation = NULL;
    }
}
Cephei::QL::Math::Interpolations::CSABRInterpolation::!CSABRInterpolation ()
{
    if (_ppSABRInterpolation != NULL)
    {
	    delete _ppSABRInterpolation;
    }
}
QuantLib::SABRInterpolation& Cephei::QL::Math::Interpolations::CSABRInterpolation::GetReference ()
{
    if (_ppSABRInterpolation == NULL) throw gcnew NativeNullException ();
	return **_ppSABRInterpolation;
}
boost::shared_ptr<QuantLib::SABRInterpolation>& Cephei::QL::Math::Interpolations::CSABRInterpolation::GetShared ()
{
    if (_ppSABRInterpolation == NULL) throw gcnew NativeNullException ();
	return *_ppSABRInterpolation;
}
QuantLib::SABRInterpolation* Cephei::QL::Math::Interpolations::CSABRInterpolation::GetPointer ()
{
    if (_ppSABRInterpolation == NULL) throw gcnew NativeNullException ();
	return &**_ppSABRInterpolation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SABRInterpolation>& Cephei::QL::Math::Interpolations::CSABRInterpolation::GetHandle ()
{
	if (_phSABRInterpolation == NULL)
	{
		_phSABRInterpolation = new Handle<QuantLib::SABRInterpolation> (*_ppSABRInterpolation);
	}
	return *_phSABRInterpolation;
}
#endif
bool Cephei::QL::Math::Interpolations::CSABRInterpolation::HasNative () 
{
	return (_ppSABRInterpolation != NULL);
}

Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Alpha::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->alpha ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Beta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->beta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Math::Optimization::EndCriteria::TypeEnum Cephei::QL::Math::Interpolations::CSABRInterpolation::EndCriteria::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::EndCriteria::Type _rv = (QuantLib::EndCriteria::Type)(*_ppSABRInterpolation)->endCriteria ( );   
        QL::Math::Optimization::EndCriteria::TypeEnum _nrv = (QL::Math::Optimization::EndCriteria::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Expiry::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->expiry ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Forward::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->forward ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Math::Interpolations::CSABRInterpolation::InterpolationWeights::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppSABRInterpolation)->interpolationWeights ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::MaxError::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->maxError ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Nu::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->nu ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::Rho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->rho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CSABRInterpolation::RmsError::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSABRInterpolation)->rmsError ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

